package com.youlai.boot.system.converter;

import org.springframework.stereotype.Component;
import com.youlai.boot.system.model.entity.DailySalesReport;
import com.youlai.boot.system.model.form.DailySalesReportForm;
import com.youlai.boot.system.model.vo.DailySalesReportVO;
import com.youlai.boot.system.model.dto.DailySalesReportExcelDTO;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TP运营日报对象转换器实现
 *
 * @author xiheforever
 * @since 2025-06-03
 */
@Component
public class DailySalesReportConverterImpl implements DailySalesReportConverter {

    @Override
    public DailySalesReportForm toForm(DailySalesReport entity) {
        if (entity == null) {
            return null;
        }
        
        DailySalesReportForm form = new DailySalesReportForm();
        form.setId(entity.getId());
        form.setReportDate(entity.getReportDate());
        form.setAverageTransactionValue(entity.getAverageTransactionValue());
        form.setConversionRate(entity.getConversionRate());
        form.setMonthlySalesTarget(entity.getMonthlySalesTarget());
        form.setCurrentSalesAmount(entity.getCurrentSalesAmount());
        form.setSalesProgress(entity.getSalesProgress());
        form.setTimeProgress(entity.getTimeProgress());
        form.setGmvYoyChange(entity.getGmvYoyChange());
        form.setAvgConversionRate(entity.getAvgConversionRate());
        form.setAvgTransactionValue(entity.getAvgTransactionValue());
        form.setTotalSalesQuantity(entity.getTotalSalesQuantity());
        
        return form;
    }

    @Override
    public DailySalesReport toEntity(DailySalesReportForm formData) {
        if (formData == null) {
            return null;
        }
        
        DailySalesReport entity = new DailySalesReport();
        entity.setId(formData.getId());
        entity.setReportDate(formData.getReportDate());
        entity.setAverageTransactionValue(formData.getAverageTransactionValue());
        entity.setConversionRate(formData.getConversionRate());
        entity.setMonthlySalesTarget(formData.getMonthlySalesTarget());
        entity.setCurrentSalesAmount(formData.getCurrentSalesAmount());
        entity.setSalesProgress(formData.getSalesProgress());
        entity.setTimeProgress(formData.getTimeProgress());
        entity.setGmvYoyChange(formData.getGmvYoyChange());
        entity.setAvgConversionRate(formData.getAvgConversionRate());
        entity.setAvgTransactionValue(formData.getAvgTransactionValue());
        entity.setTotalSalesQuantity(formData.getTotalSalesQuantity());
        
        return entity;
    }

    @Override
    public DailySalesReportVO toVO(DailySalesReport entity) {
        if (entity == null) {
            return null;
        }
        
        DailySalesReportVO vo = new DailySalesReportVO();
        vo.setId(entity.getId());
        vo.setReportDate(entity.getReportDate());
        vo.setAverageTransactionValue(entity.getAverageTransactionValue());
        vo.setConversionRate(entity.getConversionRate());
        vo.setMonthlySalesTarget(entity.getMonthlySalesTarget());
        vo.setCurrentSalesAmount(entity.getCurrentSalesAmount());
        vo.setSalesProgress(entity.getSalesProgress());
        vo.setTimeProgress(entity.getTimeProgress());
        vo.setGmvYoyChange(entity.getGmvYoyChange());
        vo.setAvgConversionRate(entity.getAvgConversionRate());
        vo.setAvgTransactionValue(entity.getAvgTransactionValue());
        vo.setTotalSalesQuantity(entity.getTotalSalesQuantity());
        
        // 设置更新时间，如果实体没有更新时间则使用当前时间
        vo.setUpdateTime(entity.getUpdateTime() != null ? 
                entity.getUpdateTime() : LocalDateTime.now());
        
        return vo;
    }

    @Override
    public List<DailySalesReportExcelDTO> entity2ExcelDTO(List<DailySalesReport> entities) {
        if (entities == null || entities.isEmpty()) {
            return List.of();
        }
        return entities.stream()
                .map(this::entity2ExcelDTO)
                .collect(Collectors.toList());
    }

    @Override
    public DailySalesReportExcelDTO entity2ExcelDTO(DailySalesReport entity) {
        if (entity == null) {
            return null;
        }
        
        DailySalesReportExcelDTO dto = new DailySalesReportExcelDTO();
        dto.setReportDate(entity.getReportDate());
        dto.setAverageTransactionValue(entity.getAverageTransactionValue());
        dto.setConversionRate(entity.getConversionRate());
        
        // 将金额从元转换为万元
        dto.setMonthlySalesTarget(entity.getMonthlySalesTarget() != null ? 
                entity.getMonthlySalesTarget().divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP) : null);
        dto.setCurrentSalesAmount(entity.getCurrentSalesAmount() != null ? 
                entity.getCurrentSalesAmount().divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP) : null);
        
        // 百分比字段需要除以100转换为小数形式，因为@NumberFormat会自动处理百分比显示
        dto.setSalesProgress(entity.getSalesProgress() != null ? 
                entity.getSalesProgress().divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP) : null);
        dto.setTimeProgress(entity.getTimeProgress() != null ? 
                entity.getTimeProgress().divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP) : null);
        dto.setGmvYoyChange(entity.getGmvYoyChange() != null ? 
                entity.getGmvYoyChange().divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP) : null);
        dto.setAvgConversionRate(entity.getAvgConversionRate() != null ? 
                entity.getAvgConversionRate() : null);
        
        dto.setAvgTransactionValue(entity.getAvgTransactionValue());
        dto.setTotalSalesQuantity(entity.getTotalSalesQuantity());
        
        return dto;
    }

    @Override
    public DailySalesReport excelDTO2Entity(DailySalesReportExcelDTO excelDTO) {
        if (excelDTO == null) {
            return null;
        }
        
        DailySalesReport entity = new DailySalesReport();
        entity.setReportDate(excelDTO.getReportDate());
        entity.setAverageTransactionValue(excelDTO.getAverageTransactionValue());
        entity.setConversionRate(excelDTO.getConversionRate());
        
        // 将万元转换为元
        entity.setMonthlySalesTarget(excelDTO.getMonthlySalesTarget() != null ? 
                excelDTO.getMonthlySalesTarget().multiply(BigDecimal.valueOf(10000)) : null);
        entity.setCurrentSalesAmount(excelDTO.getCurrentSalesAmount() != null ? 
                excelDTO.getCurrentSalesAmount().multiply(BigDecimal.valueOf(10000)) : null);
        
        // 小数形式转换为百分比形式
        entity.setSalesProgress(excelDTO.getSalesProgress() != null ? 
                excelDTO.getSalesProgress().multiply(BigDecimal.valueOf(100)) : null);
        entity.setTimeProgress(excelDTO.getTimeProgress() != null ? 
                excelDTO.getTimeProgress().multiply(BigDecimal.valueOf(100)) : null);
        entity.setGmvYoyChange(excelDTO.getGmvYoyChange() != null ? 
                excelDTO.getGmvYoyChange().multiply(BigDecimal.valueOf(100)) : null);
        
        entity.setAvgConversionRate(excelDTO.getAvgConversionRate());
        entity.setAvgTransactionValue(excelDTO.getAvgTransactionValue());
        entity.setTotalSalesQuantity(excelDTO.getTotalSalesQuantity());
        
        return entity;
    }

    @Override
    public List<DailySalesReport> excelDTO2Entity(List<DailySalesReportExcelDTO> excelDTOs) {
        if (excelDTOs == null || excelDTOs.isEmpty()) {
            return List.of();
        }
        return excelDTOs.stream()
                .map(this::excelDTO2Entity)
                .collect(Collectors.toList());
    }
} 